﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Common;
using Model;

namespace API.Controllers
{
    /// <summary>
    /// 攻略分类
    /// </summary>
    [RoutePrefix("api/StrategType")]
    public class StrategTypeController : ApiController
    {
        B_StrategType _bst;
        public StrategTypeController()
        {
            _bst = new B_StrategType();
        }

        /// <summary>
        /// 获取所有攻略分类：分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAll")]
        public IHttpActionResult GetAll(int page,int size)
        {
            var data = from st in _bst.GetAll()
                       select new Param_StrategType
                       {
                           STypeID = st.STypeID,
                           STypeName = st.STypeName,
                           STPower = st.STPower,
                           Show = st.Show,
                           CreateTime = st.CreateTime
                       };
            return Ok(new Result<List<Param_StrategType>>
            {
                Data = data.Skip((page-1)*size).Take(size).ToList(),
                Msg=data.Count().ToString()
            });
        }

        /// <summary>
        /// 获取所有攻略分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAll")]
        public IHttpActionResult GetAll()
        {
            var data = from st in _bst.GetAll()
                       select new Param_StrategType
                       {
                           STypeID = st.STypeID,
                           STypeName = st.STypeName,
                           STPower = st.STPower,
                           Show = st.Show,
                           CreateTime = st.CreateTime
                       };
            return Ok(new Result<List<Param_StrategType>>
            {
                Data = data.ToList()
            });
        }
        /// <summary>
        /// 创建攻略分类
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Add")]
        public IHttpActionResult Add(StrategType type)
        {
            try
            {
                type.CreateTime = DateTime.Now;
                _bst.Add(type);
                return Ok(new Result<object>());
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }
        /// <summary>
        /// 删除攻略分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Delete/{id}")]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                if (new B_Strateg().GetAll().Count(x => x.STypeID == id) > 0)
                {
                    return Ok(new Result<object>()
                    {
                        Code = 500,
                        Msg = "无法删除"
                    });
                }
                _bst.Delete(id);
                return Ok(new Result<object>());
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }
        /// <summary>
        /// 根据编号ID查询攻略分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Find/{id}")]
        public IHttpActionResult Find(int id)
        {
            try
            {
                var data = _bst.FindById(id);
                if (data == null) 
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据为空"
                    });
                }
                else
                {
                    return Ok(new Result<Param_StrategType>
                    {
                        Data=new Param_StrategType
                        {
                            STypeID = data.STypeID,
                            STypeName = data.STypeName,
                            STPower = data.STPower,
                            Show = data.Show,
                            CreateTime = data.CreateTime
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }
        /// <summary>
        /// 根据编号ID和type修改攻略分类
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Update/{id}")]
        public IHttpActionResult Update(int id,StrategType type)
        {
            try
            {
                var data = _bst.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据为空"
                    });
                }
                else
                {
                    type.CreateTime = DateTime.Now;
                    _bst.Update(id, type);
                    return Ok(new Result<object>());
                }
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }
    }
}
